<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS.DS."CoreUser.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS.DS."CoreUser.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."CoreUser.php";

class UserController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function addAction()
    {   
    	//Create form enter info for new Page
    	$form = new Application_Form_CoreUser();
    	
    	//Set label for the button
        $form->submit->setLabel('Add');
        
        //Set client view page form  = $form
        $this->view->form = $form;
        $this->view->bodyCopy = "<p >Nhập thông tin user:</p>";
        $created_date = date('Y-m-d H:i:s');
        //If can get client request 
        if ($this->getRequest()->isPost()) 
        {
        	//Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))   //If data is valid 
            {
            	//Get data one by one
                $role_id = (int)$form->getValue('role_id');
                $user_name = $form->getValue('user_name');
                $password = $form->getValue('password');
                $full_name = $form->getValue('full_name');
                $email = $form->getValue('email');
			//	$is_active= (int)$form->getValue('is_active');
			//	$is_online= (int)$form->getValue('is_online');
                
                //Create new  CoreUser object from datas
                $coreUser = new CoreUser(null, $role_id, $user_name, $password, $full_name, $email ,0, $created_date,null,0);
                
                //Create  CoreUser model 
                $CoreUsers = new End_Models_CoreUser();
                
                //Use insert function from  CoreUser model
                $CoreUsers->_insert($coreUser);
                
                //Move to index page
                $this->_helper->redirector('index');
            } else 
            {
            	//Return form
                $form->populate($formData);
            }
        }
    }
    
    public function deleteAction()
    {	
    	if ($this->getRequest()->isPost()) 
    	{
            $del = $this->getRequest()->getPost('del'); 
    		if ($del == 'Yes') 
            {
                $user_id = (int)$this->getRequest()->getPost('user_id');
                $CoreUsers = new End_Models_CoreUser();
                $CoreUsers->_delete($user_id);
            }
            $this->_helper->redirector('index');
        } else 
        {
            $user_id = $this->_getParam('user_id', 0);
            $CoreUsers = new End_Models_CoreUser();
            $this->view->CoreUser = Utility_Converter::objectToArray($CoreUsers->_find($user_id));
        }
    }
    
    public function editAction()
    {
    	$user_id = $this->_getParam('user_id');
    	$form = new Application_Form_CoreUser();
        $form->submit->setLabel('Save');
     //   $logged_in_date= date('Y-m-d H:i:s');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {	
               	$role_id = (int)$form->getValue('role_id');
                $user_name = $form->getValue('user_name');
                $password = $form->getValue('password');
                $full_name = $form->getValue('full_name');
                $email = $form->getValue('email');
	//			$is_active= (int)$form->getValue('is_active');
	//			$is_online= (int)$form->getValue('is_online');
   		
				$coreUser = new CoreUser($user_id, $role_id, $user_name, $password, $full_name, $email ,0,null,null,0);	
                $CoreUsers = new End_Models_CoreUser();						
                $CoreUsers->_update($coreUser);								
                $this->_helper->redirector('index');				
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $user_id = $this->_getParam('user_id', 0);
            if ($user_id > 0) 
            {
                $CoreUsers = new End_Models_CoreUser();
                $form->populate(Utility_Converter::objectToArray($CoreUsers->_find($user_id)));
            }
        }
    }
    
    public function indexAction()
    {
    	$modelCoreUser = new End_Models_CoreUser();
		$coreUser = $modelCoreUser->fetchAll();
		$this->view->CoreUser = $coreUser;
    }
    
}







